package cn.com.dycc.party_work.education_log.service;

import java.util.Arrays;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Service;

import cn.com.dycc.party_work.education_log.dao.EducationLogDao;
import cn.com.dycc.party_work.education_log.entity.EducationLog;
import cn.com.dycc.party_work.education_log.entity.EducationLogInfo;
import cn.com.dycc.party_work.education_log.param.EducationLogParam;
import cn.com.dycc.util.DateUtil;
import cn.com.dycc.util.Paging;

/**
 * 
 * Description: 党员教育学习日志
 * Author: Liket
 * Version: 1.0
 * Create Date Time: 2018年7月4日 上午9:14:14.
 * Update Date Time: 
 * @see
 */
@Service("educationLogService")
public class EducationLogServiceImpl implements EducationLogService{
	@Resource
	private EducationLogDao educationLogDao;
	
	@Resource(name="paging")
	private Paging paging;

	public int addEduLog(EducationLog eduLog, HttpServletRequest req) throws EduLogNotFoundException {
		HttpSession session = req.getSession();
		String user_id = (String) session.getAttribute("id");
		String dept_id = (String) session.getAttribute("dept_id");
		if(user_id==null){
			throw new EduLogNotFoundException("请重新登录");
		}
		if(eduLog==null){
			throw new EduLogNotFoundException("请填写日志");
		}		
		if(eduLog.getEducation_id()==null||eduLog.getEducation_id().trim().isEmpty()){
			throw new EduLogNotFoundException("填写学习任务");
		}
		/*if(eduLog.getRead_date()==null||eduLog.getRead_date().trim().isEmpty()){
			throw new EduLogNotFoundException("填写学习时间");
		}*/
		
		EducationLog oldEdu = educationLogDao.findEduLogById(eduLog.getEducation_id(), user_id);
		if(oldEdu!=null){
			return 0;
		}
		eduLog.setRead_date(DateUtil.getNowDate(DateUtil.TIME_FMT));
		eduLog.setUser_id(user_id);
		eduLog.setDept_id(dept_id);
		eduLog.setCreate_date(DateUtil.getNowDate(DateUtil.TIME_FMT));
		eduLog.setOrg_id((String)session.getAttribute("org_id"));
		eduLog.setOpt_user_id(user_id);
		
		int n = educationLogDao.addEduLog(eduLog);
		if(n!=1){
			throw new EduLogNotFoundException("添加失败");
		}
		return n;
	}

	public EducationLog findEduLog(String log_id, HttpServletRequest req) throws EduLogNotFoundException {
		HttpSession session = req.getSession();
		String org_id = (String) session.getAttribute("org_id");
		//String dept_id = (String) session.getAttribute("dept_id");
		if(org_id==null){
			throw new EduLogNotFoundException("请重新登录");
		}		
		if(log_id==null||log_id.trim().isEmpty()){
			throw new EduLogNotFoundException("请选择学习日志");
		}
		
		EducationLog edu = educationLogDao.findEduLog(log_id,org_id);
		if(edu==null){
			throw new EduLogNotFoundException("查询失败");
		}
		return edu;
	}

	public List<EducationLogInfo> findEduLogByParam(EducationLogParam param, HttpServletRequest req)
			throws EduLogNotFoundException {
		HttpSession session = req.getSession();
		String org_id = (String) session.getAttribute("org_id");
		if(org_id==null){
			throw new EduLogNotFoundException("请重新登录");
		}
		Integer page = param.getPage();
		Integer limit = param.getLimit();
		Integer start = null;
		if(limit==null&&page!=null){
			limit = paging.getPageSize();
			start=(page-1)*limit;
		}
		if(page==null){
			start = null;
			limit = null;
		}
		if(limit!=null&&page!=null){
			start = limit*(page-1);
		}		
		param.setStart(start);
		param.setOrg_id(org_id);
		List<EducationLogInfo> list = educationLogDao.findEduLogByParam(param);
		return list;
	}

	public String findEduLogCount(EducationLogParam param, HttpServletRequest req) throws EduLogNotFoundException {
		HttpSession session = req.getSession();
		String org_id = (String) session.getAttribute("org_id");
		if(org_id==null){
			throw new EduLogNotFoundException("请重新登录");
		}
		param.setOrg_id(org_id);
		String count = educationLogDao.findEduLogCount(param);
		return count;
	}

	public int deleteEduLog(String[] log_ids, HttpServletRequest req) throws EduLogNotFoundException {
		HttpSession session = req.getSession();
		String org_id = (String)session.getAttribute("org_id");
		if(org_id==null){
			throw new EduLogNotFoundException("请重新登录");
		}
		if(log_ids==null){
			throw new EduLogNotFoundException("请选择删除的结果");
		}
		if(log_ids.length<1){
			throw new EduLogNotFoundException("选择删除的结果");
		}
		List<String> list = Arrays.asList(log_ids);
		int n = educationLogDao.deleteEduLog(org_id,list);
		if(n<1){
			throw new EduLogNotFoundException("删除失败");
		}
		return n;
	}

	
}
